package com.smartfast4j.manager.common.controller;


import com.smartfast4j.common.Constants;
import com.smartfast4j.common.component.cache.CacheService;
import com.smartfast4j.common.controller.BaseController;
import com.smartfast4j.manager.common.bean.LoginUserInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Set;

/**
 * Created by gacl on 2017/3/3.
 */
public class ManagerBaseController extends BaseController {

    @Autowired
    protected CacheService cacheService; //缓存

    /**
     * 获取当前登录的系统用户
     * @return
     */
    protected LoginUserInfo getCurrLoginUser() {
        String token = getRequest().getParameter("token");
        //从缓存中获取当前登录的系统用户
        LoginUserInfo loginUser = cacheService.get(Constants.LOGIN_USER.TOKEN_PREFIX+token);
        return loginUser;
    }

    /**
     * 登录用户的ID
     * @return
     */
    protected Long getLoginUserId() {
        LoginUserInfo loginUser = getCurrLoginUser();
        if(loginUser!=null){
            return loginUser.getId();
        }
        return null;
    }

    /**
     * 登录用户所属的角色列表
     * @return
     */
    protected List<Long> getLoginUserRoleIds() {
        LoginUserInfo loginUser = getCurrLoginUser();
        if(loginUser!=null){
            return loginUser.getRoleIds();
        }
        return null;
    }

    /**
     * 登录用户所拥有的权限列表
     * @return
     */
    protected Set<String> getLoginUserPerms() {
        LoginUserInfo loginUser = getCurrLoginUser();
        if(loginUser!=null){
            return loginUser.getPerms();
        }
        return null;
    }
}
